﻿#include "MainWindow.h"
#include <QTabWidget>
#include <QToolBar>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QtMath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    buildUi();
    loadCmfOrAsk();
}

void MainWindow::buildUi()
{
    tabs_ = new QTabWidget(this);
    setCentralWidget(tabs_);

    plot1931_ = new CIEChromaticityPlot;
    plot1931_->setDiagram(DiagramKind::CIE1931_xy);

    plot1976_ = new CIEChromaticityPlot;
    plot1976_->setDiagram(DiagramKind::CIE1976_uv);

    plot2015_ = new CIEChromaticityPlot;
    plot2015_->setDiagram(DiagramKind::CIE2015_uv);

    tabs_->addTab(plot1931_, tr("CIE 1931 (x–y)"));
    tabs_->addTab(plot1976_, tr("CIE 1976 (u′–v′)"));
    tabs_->addTab(plot2015_, tr("CIE 2015 (u′–v′)"));
    connect(tabs_, &QTabWidget::currentChanged, this, &MainWindow::onTabChanged);

    auto *tb = addToolBar(tr("Overlays"));
    actSRGB_   = tb->addAction(tr("sRGB"));
    actAdobe_  = tb->addAction(tr("AdobeRGB"));
    actPlanck_ = tb->addAction(tr("Planck"));
    actIsoCCT_ = tb->addAction(tr("iso-CCT"));
    actDomWL_  = tb->addAction(tr("Dominant λ"));
    for (QAction *a : {actSRGB_, actAdobe_, actPlanck_, actIsoCCT_, actDomWL_}) {
        a->setCheckable(true);
        a->setChecked(true);
    }
    connect(actSRGB_,   &QAction::toggled, this, &MainWindow::onToggleSRGB);
    connect(actAdobe_,  &QAction::toggled, this, &MainWindow::onToggleAdobe);
    connect(actPlanck_, &QAction::toggled, this, &MainWindow::onTogglePlanck);
    connect(actIsoCCT_, &QAction::toggled, this, &MainWindow::onToggleIsoCCT);
    connect(actDomWL_,  &QAction::toggled, this, &MainWindow::onToggleDomWL);

    setWindowTitle(tr("CIE Chromaticity – 1931 / 1976 / 2015"));
    resize(1024, 800);
}

void MainWindow::loadCmfOrAsk()
{
    cmf1931Ok_ = plot1931_->loadCMF(DiagramKind::CIE1931_xy,  "E:/work/temp/demoCIE/cmf1931.csv");
    cmf2015Ok_ = plot2015_->loadCMF(DiagramKind::CIE2015_uv,  "E:/work/temp/demoCIE/cmf2015_demo.csv");
    if (cmf1931Ok_) plot1976_->loadCMF(DiagramKind::CIE1976_uv, "E:/work/temp/demoCIE/cmf1931.csv");

    if (!cmf1931Ok_) {
        const QString p = QFileDialog::getOpenFileName(this, tr("选择 cmf1931.csv"), QString(), tr("CSV Files (*.csv);;All Files (*.*)"));
        if (!p.isEmpty()) {
            cmf1931Ok_ = plot1931_->loadCMF(DiagramKind::CIE1931_xy, p);
            if (cmf1931Ok_) plot1976_->loadCMF(DiagramKind::CIE1976_uv, p);
        }
    }
    if (!cmf2015Ok_) {
        const QString p = QFileDialog::getOpenFileName(this, tr("选择 cmf2015.csv"), QString(), tr("CSV Files (*.csv);;All Files (*.*)"));
        if (!p.isEmpty()) cmf2015Ok_ = plot2015_->loadCMF(DiagramKind::CIE2015_uv, p);
    }

    if (!cmf1931Ok_) QMessageBox::warning(this, tr("提示"), tr("未能加载 cmf1931.csv，1931/1976 页面将为空。"));
    if (!cmf2015Ok_) QMessageBox::warning(this, tr("提示"), tr("未能加载 cmf2015.csv，2015 页面将为空。"));

    // 示例：1931 页面加 D65 与一个高斯谱
    if (cmf1931Ok_) {
        plot1931_->addPoint_xy(0.3127, 0.3290, "D65");
        QVector<double> wl, sp;
        for (int L=380; L<=780; L+=5) {
            wl.push_back(L);
            sp.push_back(qExp(-0.5 * qPow((L - 550)/20.0, 2.0)));
        }
        plot1931_->setSpectrum(wl, sp);
    }

    applyTogglesToAll();
}

void MainWindow::applyTogglesToAll()
{
    const bool s  = actSRGB_->isChecked();
    const bool a  = actAdobe_->isChecked();
    const bool p  = actPlanck_->isChecked();
    const bool ic = actIsoCCT_->isChecked();
    const bool d  = actDomWL_->isChecked();

    for (CIEChromaticityPlot *w : {plot1931_, plot1976_, plot2015_}) {
        if (!w) continue;
        w->setShowSRGB(s);
        w->setShowAdobeRGB(a);
        w->setShowPlanck(p);
        w->setShowIsoCCT(ic);
        w->setShowDominantLines(d);
    }
}

void MainWindow::onToggleSRGB(bool on)   { Q_UNUSED(on);   applyTogglesToAll(); }
void MainWindow::onToggleAdobe(bool on)  { Q_UNUSED(on);  applyTogglesToAll(); }
void MainWindow::onTogglePlanck(bool on) { Q_UNUSED(on);  applyTogglesToAll(); }
void MainWindow::onToggleIsoCCT(bool on) { Q_UNUSED(on);  applyTogglesToAll(); }
void MainWindow::onToggleDomWL(bool on)  { Q_UNUSED(on);  applyTogglesToAll(); }
void MainWindow::onTabChanged(int idx)   { Q_UNUSED(idx); }
